﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using ClassLibrary.DAO;
using ClassLibrary.Entity;
using System.IO;

public partial class Admin_ucControl_ucSuppliers : System.Web.UI.UserControl
{
    TblSupplierDAO supplierDAO = new TblSupplierDAO(Global.CONNECTION_STRING);

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session[Global.SESSION_ADMIN_USER] == "" || Session[Global.SESSION_ADMIN_USER] == null)
            {
                Response.Redirect("~/Admin/Login.aspx");
            }
            string id = Request.QueryString["id"];
            if (id == null || id == "")
            {
                LoadSuppliers();
            }
            else
            {
                LoadDetailSupp();
                LoadSuppliers();
            }
            
        }
    }
    private void LoadSuppliers()
    {
        try
        {
            grdMenuParent.DataSource = supplierDAO.GetAll();
            grdMenuParent.DataBind();
        }
        catch (Exception ex)
        {
            Session[Global.SESSION_ERROR] = ex;
            Response.Redirect("~/Error.aspx", true);
        }
    }

    private void LoadDetailSupp()
    {
        try
        {
            string id = Request.QueryString["id"];

            TblSuppliers suppliers = supplierDAO.GetSuppliersByID(Global.ConvertToInt(id));
            txtgioithieu.Content = suppliers.Suppliers;
            txtnhacc.Text = suppliers.Suppliers;
        }
        catch (Exception ex)
        {
            Session[Global.SESSION_ERROR] = ex;
            Response.Redirect("~/Error.aspx", true);
        }
    }
    //Kiem tra ten nha cung cap 
    private bool Check_SuppliersExists()
    {
        bool bExists = false;
        try
        {

            bExists = supplierDAO.check_suppliers(txtnhacc.Text);
            System.Threading.Thread.Sleep(3000);
        }
        catch (Exception ex)
        {
            Session[Global.SESSION_ERROR] = ex;
            Response.Redirect("~/Error.aspx", true);
        }
        return bExists;
    }

    protected void btAdd_Click(object sender, EventArgs e)
    {
        bool flag = false;
        try
        {
            TblSuppliers Suppliers = new TblSuppliers();
            string id = Request.QueryString["id"];
            Suppliers.Suppliers = txtnhacc.Text;
            Suppliers.SuppDescription = Server.HtmlDecode(txtgioithieu.Content);

            string filename = upImage.FileName;

            string path = Server.MapPath("~\\Admin\\images\\Items\\");

            if (id == null || id == "")
            {
                if (Check_SuppliersExists())
                {
                    lb_message.Text = "Nhà cung cấp này đã tồn tại.";
                    return;
                }
                else
                {
                    if (File.Exists(path + "\\" + filename))
                    {
                        ScriptManager.RegisterStartupScript(btAdd, btAdd.GetType(), "alert", "alert(' Hình này đã tồn tại!');", true);
                        return;
                    }
                    upImage.SaveAs(path + "\\" + filename);

                    Suppliers.ImageURL = filename;
                    Suppliers.IDSupplie = 0;
                    supplierDAO.InUpSupplier(Suppliers, 1);
                }
            }
            else
            {
               
                int id1 = Global.ConvertToInt(Request.QueryString["id"]);
                TblSuppliers Supplier = supplierDAO.GetSuppliersByID(id1);
                if (Supplier != null)
                {
                    string fname = Supplier.ImageURL;
                    if (fname != null)
                    {
                        File.Delete(Server.MapPath("images\\items\\" + fname));
                    }

                    Suppliers.IDSupplie = id1;
                    Suppliers.Suppliers = txtnhacc.Text;
                    Suppliers.SuppDescription = Server.HtmlDecode(txtgioithieu.Content);
                    Suppliers.ImageURL = filename;
                    //Suppliers.ImageURL = "";
                    upImage.SaveAs(Server.MapPath("images\\items\\" + filename));
                
                    supplierDAO.InUpSupplier(Suppliers, 2);
                }

            }
            flag = true;
        }
        catch (Exception ex)
        {
            Session[Global.SESSION_ERROR] = ex;
            Response.Redirect(Global.ERROR_PAGE_ADMIN);
        }
        if (flag)
        {
            LoadSuppliers();
            // Panel1.Visible = false;
        }
    }
    protected void grdMenuParent_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grdMenuParent.PageIndex = e.NewPageIndex;
        LoadSuppliers();
    }
}
